﻿// See https://aka.ms/new-console-template for more information
using Jint;
using Jint.Runtime.Interop;
using System.Security.AccessControl;

internal class Program
{
    private static void Main(string[] args)
    {
        {

            var engine = new Engine().SetValue("log", new Action<object>(Console.WriteLine));
            engine.Execute(@"
                        function hello() { 
                            log('Hello World');
                        };
                         hello();
                     ");
        }

        {
            var p = new TheType
            {
                Name = "Mickey Mouse"
            };

            var engine = new Engine()
                .SetValue("p", p)
                .Execute("p.Name = 'Minnie'"); // converts the value to .NET

            var @bool = p.Name == "Minnie";
            Console.WriteLine(p.Name);
        }
        {
            var add = new Engine().Execute("function add(a, b) { return a + b; }")
                                  .GetValue("add");
            var result = add.Invoke(1, 2); // -> 3
            Console.WriteLine(result);
        }
        {
            var engine = new Engine(cfg => cfg.AllowClr());
            var file = new StreamWriter("C:\\Img\\log.txt");
            file.WriteLine("Hello World !");
            file.Dispose();
            engine.SetValue("TheType", TypeReference.CreateTypeReference(engine, typeof(TheType)))
                  .Execute("TheType.Name ='123'");

        }
        {
            var engine = new Engine(cfg => cfg.AllowClr(typeof(TheType).Assembly));


        }
        {
            // 读取外部 JavaScript 文件内容
            string externalJsCode = File.ReadAllText("doT.js");
            //// 创建 Jint 引擎实例
            //var engine = new JintJsEngine();
            //// 执行外部 JavaScript 代码
            //engine.Execute(externalJsCode);
        }
        {
            // 读取 doT 模板文件内容
            string dotTemplate = File.ReadAllText("hello.dot");
            // 创建 Jint 引擎实例
            var engine = new Engine();
            try
            {
                // 引入 doT.js 库
                engine.Execute(File.ReadAllText("doT.js"));
                // 定义 doT 模板
                engine.Execute($"var template = doT.template('{dotTemplate}');");
                string tempData = "{\"name\": \"John\", \"age\": 30, \"city\": \"New York\"}";
                // 将 JSON 字符串解析为 JavaScript 对象
                engine.Execute("var jsonData = JSON.parse('" + tempData.Replace("'", "\\'") + "');");

                // 调用 doT 模板并传递数据
                var result = engine.Execute("template(jsonData);").GetCompletionValue().AsString();
                // 输出结果
                Console.WriteLine(result);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error executing JavaScript: {ex.Message}");
            }

        }
        {
            // 读取 doT 模板文件内容
            string dotTemplate = File.ReadAllText("Template.dot");
            //string dotData = File.ReadAllText("data.dot").Replace("'", "\\'").Replace("\r", "").Replace("\n", "\\n");
            // 创建 Jint 引擎实例
            var engine = new Engine();
            try
            {
                // 引入 doT.js 库
                engine.Execute(File.ReadAllText("doT.js"));
                // 定义 doT 模板
                engine.Execute($"var template = doT.template('{dotTemplate}');");
                // 将 JSON 字符串解析为 JavaScript 对象
                //engine.Execute("var jsonData = JSON.parse('" + data + "');");
                //// 调用 doT 模板并传递数据
                var result = engine.Execute($"template({ data});").GetCompletionValue().AsString();
                // 输出结果
                Console.WriteLine(result);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error executing JavaScript: {ex.Message}");
            }

        }


    }



    public static string data = "{\r\n  \"Usings\": [\r\n    \"using System;\",\r\n    \"using System.Reflection;\"\r\n  ],\r\n  \"NameSpace\": \"APP\\r\\n\",\r\n  \"Modifier\": \"public\",\r\n  \"DefName\": \"AutoTemplate\",\r\n  \"Attributes\": [\r\n    {\r\n      \"DefName\": \"DotTemplate\",\r\n      \"Parameters\": [\r\n        {\r\n          \"Type\": \"AttributeArgumentSyntax\",\r\n          \"DefName\": \"Template.dot\"\r\n        },\r\n        {\r\n          \"Type\": \"AttributeArgumentSyntax\",\r\n          \"DefName\": \"APP/DotTemplate\"\r\n        }\r\n      ]\r\n    }\r\n  ],\r\n  \"Methods\": [\r\n    {\r\n      \"Modifier\": \"public\",\r\n      \"DefName\": \"Method1\",\r\n      \"Type\": \"void\",\r\n      \"Parameters\": []\r\n    },\r\n    {\r\n      \"Modifier\": \"public\",\r\n      \"DefName\": \"Method2\",\r\n      \"Type\": \"int\",\r\n      \"Parameters\": [\r\n        {\r\n          \"Type\": \"int\",\r\n          \"DefName\": \"num\"\r\n        }\r\n      ]\r\n    },\r\n    {\r\n      \"Modifier\": \"public\",\r\n      \"DefName\": \"Method3\",\r\n      \"Type\": \"int\",\r\n      \"Parameters\": [\r\n        {\r\n          \"Type\": \"string\",\r\n          \"DefName\": \"str\"\r\n        }\r\n      ]\r\n    },\r\n    {\r\n      \"Modifier\": \"public\",\r\n      \"DefName\": \"Method4\",\r\n      \"Type\": \"int\",\r\n      \"Parameters\": [\r\n        {\r\n          \"Type\": \"string\",\r\n          \"DefName\": \"str\"\r\n        }\r\n      ]\r\n    }\r\n  ],\r\n  \"Fileds\": [\r\n    {\r\n      \"Modifier\": \"public\",\r\n      \"DefName\": \"FiledName1\",\r\n      \"Type\": \"int\"\r\n    },\r\n    {\r\n      \"Modifier\": \"public\",\r\n      \"DefName\": \"FiledName\",\r\n      \"Type\": \"int\"\r\n    }\r\n  ],\r\n  \"Propertys\": [\r\n    {\r\n      \"Modifier\": \"public\",\r\n      \"DefName\": \"PropertyName\",\r\n      \"Type\": \"string?\"\r\n    }\r\n  ]\r\n}".Replace("'", "\\'").Replace("\r", "").Replace("\n", "");


    public class TheType
    {
        public string Name { get; set; }


        public TheType()
        {
            Console.WriteLine("TheType被构建了");
        }
    }

}